<?php
/**
 * Created by PhpStorm.
 * User: wjj
 * Date: 2016/11/1
 */

namespace app\logic;


use app\models\Paper;
use app\models\PaperTest;
use app\models\Test;
use yii;


class PaperLogic
{
    public static $input_type_list = ['下拉框','输入框','单选项','多选项'];

    public function getPaper($paper_type)
    {
        $paper = Paper::find()->where(['paper_type'=>$paper_type,'is_del'=>0]);
        $lists = $paper->asArray()->all();
        $return = [
            'lists' => $lists,
            'pager' => [
                'total' => $paper->count(),
                'url' => 'paper/index'
            ]
        ];
        return $return;
    }

    public function addPaper($params,$uploadresult){
        $paper = new Paper();
        $paper->paper_name = $params['paper_name'];
        $paper->paper_type = $params['paper_type'];
        $paper->paper_pic = $uploadresult;
        return $paper->save();

    }


    public function getById($id)
    {
        return Paper::find()->where(['paper_id' => $id])->asArray()->one();
    }

    public function getPaperByType($paper_type){
        return Paper::find()->where(['paper_type' => $paper_type,'is_del'=>0])->asArray()->all();
    }

    public function editById($id, $params,$uploadresult){
        if(!$paper = Paper::findOne(['paper_id'=>$id])) return false;
        $paper->paper_name = $params['paper_name'];
        if($uploadresult)
            $paper->paper_pic = $uploadresult;
        return $paper->save();
    }

    public function delete($id){
        if(!$paper = Paper::findOne($id)) return false;
        /*$paperTest = PaperTest::findAll(['paper_id'=>$id]);
        foreach($paperTest AS $value){
            $value->delete();
        }
        return $paper->delete();*/
        $paper->is_del = 1;
        return $paper->save();
    }

    public function copy($id){
        if(!$paper_temp = Paper::find()->where(['paper_id'=>$id])->asArray()->one()) return false;
        $paper = new Paper();
        $paper->paper_name = $paper_temp['paper_name'].'复制';
        $paper->paper_type = $paper_temp['paper_type'];
        $paper->paper_pic = $paper_temp['paper_pic'];
        $paper->save();
        $paperTest_temp = PaperTest::find()->where(['paper_id'=>$id])->asArray()->all();
        foreach($paperTest_temp AS $value){
            $test_old = Test::find()->where(['test_id'=>$value['test_id']])->asArray()->one();
            $test_new = new Test();
            $test_new->test_title = $test_old['test_title'];
            $test_new->test_type = $test_old['test_type'];
            $test_new->test_detail = $test_old['test_detail'];
            $test_new->test_options = $test_old['test_options'];
            $test_new->test_answer = $test_old['test_answer'];
            $test_new->test_extend = $test_old['test_extend'];
            $test_new->add_time = date('Y-m-d H:i:s');
            $test_new->answer_option_num = $test_old['answer_option_num'];
            $test_new->is_del = $test_old['is_del'];
            $test_new->save();
            $papeTest = new PaperTest();
            $papeTest->paper_id = $paper->paper_id;
            $papeTest->test_id = $test_new->test_id;
            $papeTest->sort = $value['sort'];
            $papeTest->save();
        }
        return true;
    }


}